﻿using Common;
using MySql.Data.MySqlClient;
using NB.MGJ.FrameWork;
using NB.MGJ.Model;
using NB.MGJ.Model.AntiFake;
using NB.MGJ.Model.CodeManage;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace NB.MGJ.Service.AntiFake {
public class QueryStatisticsService {
	
	  public BaseRepository _base= new BaseRepository();

	  #region 导出查询
      public IEnumerable<CodeLog> GetAllQueryCodeLog(string search,string code)
      {
          var rs = string.Format("SELECT * FROM nblogmain.`CodeLog{0}` where Valid=1", code);
		  if (!string.IsNullOrWhiteSpace(search)) {
			  rs +=" and Fw_Code like '%" + search + "%'" ;
		  }
          return _base.ExecuteSqlCollection<CodeLog>(rs);
	  }
      public Organization_Enterprise Enterparise(int Eid)
      {
          string sql = "SELECT*from organization_enterprise where ID=" + Eid + "";
          var result = _base.ExecuteSqlCollection<Organization_Enterprise>(sql).FirstOrDefault();
          return result;
      }
	  #endregion

	  #region 分页数据显示
      public PagedList<CodeLog> GetQueryCodeLogService(ref PagerModel pager,string ecode,DetailSearch Smodel)
      {
          pager.FldName = @"*";
          pager.TblName = @"( SELECT Fw_SCode,a.ID,b.Fw_Code,Valid,QueryTime,QueryType,QueryProvince,QueryCity,ProductId,QueryIp,EnterpriseName,EnterpriseID  FROM nblogmain.`codelog" + ecode + "` b LEFT JOIN organization_enterprise a on a.ID=b.EnterpriseId ) s";
          string StrCondition = "   1=1 ";

          if (string.IsNullOrWhiteSpace(pager.StrCondition))
          {
              StrCondition = "   1=1  ";
          }
          else
          {
              StrCondition = pager.StrCondition;
          }

          if (Smodel.Fw_SCode!= null)
          {
              StrCondition += "  and (Fw_SCode like '%" + Smodel.Fw_SCode + "%')";
          }
          if (Smodel.QueryType != 0 || Smodel.QueryType == 1 || Smodel.QueryType == 2 || Smodel.QueryType == 3 || Smodel.QueryType == 4)
          {
              StrCondition += " and (QueryType like '%" + Smodel.QueryType + "%')";
          }        
         
          if (Smodel.StartDate != "" && Smodel.StartDate != null)
          {
              StrCondition += " and  (left(QueryTime,10) >= '" + Smodel.StartDate + "')";
          }
          if (Smodel.EndDate != "" && Smodel.EndDate != null)
          {
              StrCondition += " and  (left(QueryTime,10) <= '" + Smodel.EndDate + "')";
          }
          pager.StrCondition = StrCondition;
          if (string.IsNullOrEmpty(pager.FldSort) || pager.FldSort == " ")
              pager.FldSort = " s.QueryTime desc ";
          MySqlParameter[] parameters = _base.SqParameterRefactor(ref pager);
          IEnumerable<CodeLog> resultList = _base.ExecuteStoredProcedureCollection<CodeLog>("Common_PageList", parameters);
          return new PagedList<CodeLog>(resultList.ToList(), pager.Page, pager.PageSize, 1, Convert.ToInt32(parameters[6].Value));
      }
		  #endregion
      public string search(int eid)
      {
          try
          {

              string strSql = "SELECT Code from organization_enterprise where ID='" + eid + "'";
              return _base.ExecuteText<string>(strSql);
          }
          catch
          {
              return null;
          }
      }
      public int HasFormService(string code)
      { 
          try
                  {
                      var _connectionString = ConfigurationManager.ConnectionStrings["mgj_basemysql"].ConnectionString;
                      if (!string.IsNullOrWhiteSpace(_connectionString))
                      {
                          using (MySqlConnection conn = new MySqlConnection(_connectionString))
                          {
                              using (MySqlCommand cmd = conn.CreateCommand())
                              {
                                  conn.Open();
                                 // string fwcoded = string.Empty;
                                 // fwcoded = MiMa.Jiemi(code.Trim(), AppConst.DigitCodeKey);
                                 //fwcoded = fwcoded.Substring(0, 6);
                                 // cmd.CommandType = CommandType.Text;
                                  cmd.CommandText = string.Format("SHOW TABLES FROM nblogmain LIKE 'codelog{0}'", code);
                                  object _show = cmd.ExecuteScalar();
                                  if (_show != null)
                                  {
                                      cmd.CommandText = string.Format(" select count(1) from nblogmain.codelog{0}",code);
                                      return Convert.ToInt32(cmd.ExecuteScalar());
                                  }
                              }
                          }
                      }
                  }
                  catch { }
                  return 0;

      }
   
       
}
}
